//
//  BSSBasePopViewController.m
//  BrightSunnySky
//
//  Created by wanglei on 2019/5/14.
//  Copyright © 2019 PURANG. All rights reserved.
//

#import "BSSBasePopViewController.h"
#import "BSSBaseNavigationController.h"

@interface BSSBasePopViewController ()
@property (nonatomic, strong) UIWindow  *keyWindow;
@property (nonatomic, assign) BOOL      showAni;
@property (nonatomic, assign) BOOL      hidenAni;

@property (nonatomic,   weak) UIControl *actionControl;
@end

@implementation BSSBasePopViewController

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.canExitWhenTouchScreen = YES;
        _contentHeight = 250;
        self.backgroundViewAlpha = 0.3;
        self.needNavigationContainer = NO;
    }
    return self;
}
- (UIWindow *)keyWindow
{
    if (!_keyWindow) {
        UIWindow * alertWindow = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
        alertWindow.windowLevel = UIWindowLevelNormal;
        _keyWindow = alertWindow;
    }
    return _keyWindow;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor clearColor];
    
    self.backgroundView = [[UIView alloc]init];
    self.backgroundView.frame = self.view.frame;
    self.backgroundView.backgroundColor = [UIColor blackColor];
    self.backgroundView.alpha = 0;
    [self.view addSubview:self.backgroundView];
    
    
    UIControl *actionControl = [[UIControl alloc]init];
    [actionControl addTarget:self action:@selector(actionControlAction:) forControlEvents:UIControlEventTouchUpInside];
    actionControl.frame = self.view.frame;
    [self.view addSubview:actionControl];
    actionControl.enabled = self.canExitWhenTouchScreen;
    self.actionControl = actionControl;
    
    
    self.contentView = [[UIView alloc]init];
    self.contentView.backgroundColor = [UIColor whiteColor];
    
    self.contentView.frame = CGRectMake(0, self.view.frame.size.height, self.view.frame.size.width, _contentHeight);
    [self.view addSubview:self.contentView];
}


- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    if (self.showAni) {
        [UIView animateWithDuration:0.3 animations:^{
            [self showContentView];
        }];
    }
    else{
        [self showContentView];
    }
}
- (void)setContentHeight:(CGFloat)contentHeight
{
    _contentHeight = contentHeight;
    self.contentView.frame = CGRectMake(0, self.view.frame.size.height, self.view.frame.size.width, contentHeight);
}
//显示内容区域
- (void)showContentView{
    CGRect rect = self.contentView.frame;
    rect.origin.y = CGRectGetHeight(self.view.frame) - CGRectGetHeight(self.contentView.frame);
    self.contentView.frame = rect;
    self.backgroundView.alpha = self.backgroundViewAlpha;
    
}
//隐藏内容
- (void)hidenContentView{
    CGRect rect = self.contentView.frame;
    rect.origin.y = CGRectGetHeight(self.view.frame);
    self.contentView.frame = rect;
    self.backgroundView.alpha = 0;
}

- (void)destory{
    
    self.keyWindow.hidden = YES;
    [self.keyWindow resignKeyWindow];
    [self.keyWindow removeFromSuperview];
    self.keyWindow.rootViewController = nil;
    self.keyWindow = nil;
}


- (void)hiden:(BOOL)ani
{
    self.hidenAni = ani;
    if (self.hidenAni) {
        [UIView animateWithDuration:0.3 animations:^{
            [self hidenContentView];
        } completion:^(BOOL finished) {
            [self destory];
        }];
    }
    else{
        [self hidenContentView];
        [self destory];
    }
}
- (void)show:(BOOL)ani
{
    self.showAni = ani;
    [self.keyWindow makeKeyAndVisible];
    
    if (self.needNavigationContainer) {
        BSSBaseNavigationController * navigationController = [[BSSBaseNavigationController alloc]initWithRootViewController:self];
        self.window.rootViewController = navigationController;
    }
    else{
        self.window.rootViewController = self;
    }
}
- (UIWindow *)window
{
    return self.keyWindow;
}
- (void)setCanExitWhenTouchScreen:(BOOL)canExitWhenTouchScreen
{
    _canExitWhenTouchScreen = canExitWhenTouchScreen;
    self.actionControl.enabled = canExitWhenTouchScreen;
}

- (void)actionControlAction:(UIControl *)sender
{
    [self hiden:YES];
}
- (BOOL)shouldAutorotate
{
    return NO;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end

